package com.zygame.sdk.view.web;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.zygame.sdk.CmCode;
import com.zygame.sdk.ConstantValue;
import com.zygame.sdk.PluginFactory;
import com.zygame.sdk.ZyGameSDK;
import com.zygame.sdk.bean.PayResult;
import com.zygame.sdk.bean.SDKParams;
import com.zygame.sdk.bean.ZY_Toolbar;
import com.zygame.sdk.utils.log.LogHelper;
import com.zygame.sdk.utils.res.ResourceUtil;

/**
 * Created by YuXL on 2017/10/11.
 */

public class ZY_Act_WebView extends Activity {

    private WebView mWebView;
    private int ids;
    private int code = CmCode.CODE_PAY_CANCEL;
    private String payErrorMsg = "";
    private SDKParams developInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(ResourceUtil.getLayoutId(this, "zy_act_mywebview"));
        developInfo = PluginFactory.getInstance().getSDKParams(this);
        setRequestedOrientation(getOrientation() ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//设置横屏
        ZY_Toolbar main = getIntent().getParcelableExtra(ConstantValue.TOOLBARFLAG);
        mWebView = (WebView) findViewById(ResourceUtil.getId(this, "webview"));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WebView.setWebContentsDebuggingEnabled(true);
        }
        mWebView.getSettings().setDefaultTextEncodingName("UTF-8");
        mWebView.addJavascriptInterface(new ZYfor(), "WebViewJs");
//        mWebView.addJavascriptInterface(new ZYfor(), "WebViewJs");
        mWebView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
        mWebView.setWebViewClient(new MyWebClien());

        mWebView.getSettings().setLayoutAlgorithm(
                WebSettings.LayoutAlgorithm.NARROW_COLUMNS);

        mWebView.getSettings().setSavePassword(false);
        mWebView.getSettings().setAllowFileAccess(false);
        mWebView.getSettings().setAllowFileAccessFromFileURLs(false);
        mWebView.getSettings().setAllowUniversalAccessFromFileURLs(false);

        mWebView.removeJavascriptInterface("searchBoxJavaBridge_");
        mWebView.removeJavascriptInterface("accessibility");
        mWebView.removeJavascriptInterface("accessibilityTraversal");
        ids = main.ids;
        if(ids ==1){
            LogHelper.i("sss",main.url);
//            mWebView.postUrl(main.url, main.data.getBytes());
            mWebView.loadUrl(main.url+"?"+main.data);
        }else {
            mWebView.loadUrl(main.url+"?"+main.data);
        }

    }

    public boolean getOrientation() {
        if (this.developInfo == null || !this.developInfo.contains("ORIENTATION")) {
            return true;
        }

        return this.developInfo.getBoolean("ORIENTATION");
    }

//    public boolean getOrientation() {
//        if (this.developInfo == null || !this.developInfo.contains("ORIENTATION")) {
//            return true;
//        }
//
//        return this.developInfo.getBoolean("ORIENTATION");
//    }
    class ZYfor {
        @JavascriptInterface
        public void jsCloseWeb() {
            if(ids == 1){
                PayResult payResult = new PayResult();
                payResult.setResultCode(code);
                payResult.setExtension(payErrorMsg);
                ZyGameSDK.getInstance().onPayResult(payResult);
            }
            finish();
        }

        @JavascriptInterface
        public void paysuccess() {
            LogHelper.i("js", "suc");
            code = CmCode.CODE_PAY_SUCCESS;
        }

        @JavascriptInterface
        public void error(String msg) {
            payErrorMsg = msg;
            code = CmCode.CODE_PAY_FAIL;
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if(ids == 1){
            PayResult payResult = new PayResult();
            payResult.setResultCode(code);
            ZyGameSDK.getInstance().onPayResult(payResult);
        }else {
            if ((keyCode == KeyEvent.KEYCODE_BACK) && (mWebView.canGoBack())) {
                mWebView.goBack();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
    class MyWebClien extends WebViewClient{
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.startsWith("weixin://wap/pay?")) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);
                return true;
            }
            return super.shouldOverrideUrlLoading(view, url);
        }
    }

}
